import {
  PATHTYPE,
  type ProjectSearchRequest,
  type ProjectVo,
} from "@/interface/project";
import request, { http } from "@/utils/request";
import type { ProjectOverviewResponse } from "@/interface/pm/baseInfo";
import type { ProjectVORes } from "@/interface/task";
import type {
  pagerListQueryType,
  pagerListQueryTypeResponse,
} from "@/interface/common.ts";

// 项目列表
export const listProject = (
  params: ProjectSearchRequest,
  type: string = PATHTYPE.All,
): Promise<ProjectVo[]> => {
  return http.post(`/project/tab/${type}`, params);
};
export const listProjectTab = (
  params: ProjectSearchRequest,
  types: string[] = [],
): Promise<ProjectVo[]> => {
  const finalTypes = types.length ? types : [PATHTYPE.All];
  const query = finalTypes.map((t) => `type=${t}`).join("&");
  return http.post(`/project/multiTypeTab?${query}`, params);
};

// 删除项目
export const deleteProject = (id: string) => {
  return http.delete(`/project/${id}`);
};

// 获取任务详情
export const getTaskDetail = (id: string): Promise<ProjectVo> => {
  return http.get(`/task/${id}`);
};

// 任务树（按项目）
export const listTask = (
  data: ProjectSearchRequest,
): Promise<ProjectVORes[]> => {
  return request.post<any, ProjectVORes[]>("/task/tree", data);
};

// 更新任务
export const updateTask = (taskData: ProjectVORes): Promise<ProjectVORes> => {
  return http.put("/task/edit", taskData);
};

// 项目概览
export const projectOverviewAJAX = (
  id: string,
): Promise<ProjectOverviewResponse> => {
  return http.get(`/project/${id}`);
};

// 获取表头配置
export const getTableHeaderByTargetType = (
  targetType: string,
): Promise<any> => {
  return http.get(`/baseDataConfigEntity/getByTargetType/${targetType}`);
};

// 项目表头统计数据类型
export interface ProjectTitleStats {
  all: number; // 我的全部任务数
  archived: number; // 已归档
  curated: number; // 我负责的任务数
  favourite: number; // 我收藏的任务
  participated: number; // 我参与的任务数
}

// 获取项目表头统计
export const getMyProjectTitle = (
  params: ProjectSearchRequest,
  types: string[] = [],
): Promise<ProjectTitleStats> => {
  const finalTypes = types.length ? types : [PATHTYPE.All];
  const query = finalTypes.map((t) => `type=${t}`).join("&");
  return http.post(`/project/myProjectTitle?${query}`, params);
};

// 收藏项目
export const favoriteProject = (id: string) => {
  return http.put(`/project/favorite/${id}`);
};

// 取消收藏项目
export const unfavoriteProject = (id: string) => {
  return http.delete(`/project/favorite/${id}`);
};

// 任务拖拽排序
export const dragTask = (dragData: {
  asChildOfParentId: string; // 成为该id的子任务, 不传就是没有父任务，成为第一层任务
  id: string; // 任务id
}): Promise<any> => {
  return http.put("/task/drag", dragData);
};

// 删除任务
export const deleteTask = (id: string): Promise<any> => {
  return http.delete(`/task/${id}`);
};

export const getProjectList = (
  data: pagerListQueryType,
): Promise<pagerListQueryTypeResponse<ProjectVo>> => {
  return http.post(`project/list`, data);
};

// 项目管理-项目-回收/归档/恢复
export const updateProjectStatus = (
  id: string,
  sdmStatus: string,
): Promise<null> => {
  return http.put(`/project/updateStatus/${id}/${sdmStatus}`);
};
